$("#popup_3").append("<div class=\"popup_but\" onclick='hides()'>确定</div>");
/**
 * 供应/分销商地区mapping匹配操作
 * @author jing 2019-07-08
 */
//mapping点击事件

$(document).on('click', '.makes',function(){
    var kids = $(this).parent().siblings('input[name=ht_id]').val();   //当前信息
    var types = $(this).attr("data-type");   //当前操作类型
    $(".popup_butC").attr("data-type",types);
    $(".popup_butC").attr("data-id",kids);
    if (kids) {
        var Address = $(".country").html();
        popup(Address, 2);
        if(types == 'break_mapping') {
            if (breakMapping(kids) == false) {
                return false;
            }
        }
    }
    if(types == "break_mapping"){
        $("#popup_2 .popup_top").html("修改地区");
    }else{
        $("#popup_2 .popup_top").html("匹配地区");
    }
})


//mapping具体请求
$(".popup_butC").on('click',function(){
    var types = $(this).attr("data-type");
    var ids = $(this).attr("data-id");
    var areaId = $("#agent_area_id").val();
    var areaName = $("#agent_area_name").val();
    var cityId = $("#agent_city_id").val();
    var provinceId = $("#agent_province_id").val();
    if (areaId == '' || areaName == '' || cityId == '' || provinceId == '') {
        alert('地区信息不能为空!');
        return false;
    }

    $.ajax({
        url : '/admin/Mapping/fliggyInterAreaMapping',
        data : {mapping_type:types,kid:ids,agent_area_id:areaId,agent_area_name:areaName,agent_city_id:cityId,agent_province_id:provinceId},
        type : "POST",
        success:function(data){
            $(".maksa").hide();$(".popup").hide();
            popup(data.msg,3);
            window.location.reload();
            return false;
        }
    })
})




//提示的第二次隐藏
function hides(){
    $(".maksa").hide();$(".popup").hide();
}

//匹配操作后，替换数据
function showTd(ids,type_en,dataArr){
    $("input[name=ht_id][value='"+ids+"']").parent().find(".makes").attr("data-type",type_en);
    $("input[name=ht_id][value='"+ids+"']").parent().find("td[name=profit_id]").html(dataArr.profit_id);
    $("input[name=ht_id][value='"+ids+"']").parent().find("td[name=profit_name]").html(dataArr.profit_name);
    $("input[name=ht_id][value='"+ids+"']").parent().find("td[name=agent_area_id]").html(dataArr.pam_said);
    $("input[name=ht_id][value='"+ids+"']").parent().find("td[name=map_add_name]").html(dataArr.pam_saname);
    $("input[name=ht_id][value='"+ids+"']").parent().find("td[name=agent_city_id]").html(dataArr.pam_parent);
    $("input[name=ht_id][value='"+ids+"']").parent().find("td[name=agent_province_id]").html(dataArr.pam_s_province);
    $("input[name=ht_id][value='"+ids+"']").parent().find("#hotel_mapping").attr("disabled",false);
    $("input[name=ht_id][value='"+ids+"']").parent().find("#hotel_mapping").addClass("hotelMakes");
    if(type_en == "set_mapping"){
        $("input[name=ht_id][value='"+ids+"']").parent().find("#makes").attr("value","新增");
        $("input[name=ht_id][value='"+ids+"']").parent().find("td[name=map_status]").html('<span class="red">未匹配</span>');
        $("#mapping-btn").prop("name","set_mapping");
    }else{
        $("input[name=ht_id][value='"+ids+"']").parent().find("#makes").attr("value","修改");
        $("input[name=ht_id][value='"+ids+"']").parent().find("td[name=map_status]").html('<span class="purple">已匹配</span>');
        $("#mapping-btn").prop("name","break_mapping");
    }
}

//修改地区显示
function breakMapping(ids){
    $.ajax({
        url : '/admin/Mapping/mappingInfo',
        data : {kid:ids},
        type : "POST",
        async:false,
        success:function(data){
            if (data.code == 1) {
                $("#agent_area_id").val(data.content.pam_said);
                $("#agent_area_name").val(data.content.pam_saname);
                $("#agent_city_id").val(data.content.pam_parent);
                $("#agent_province_id").val(data.content.pam_s_province);
                return true;
            } else {
                alert(data.msg);
                return false;
            }
        }
    })
}

//省份联动
function countrys(obj){
    var provice = $("#countrys").val();
    if(!provice){
        $("#provices").html("<option value=''>请选择省份</option>");
        $("#citys").html("<option value=''>请选择市区</option>");
    }else{
        getAddress('',provice,"#provices");
        var cid = $("#provices").val();
        getAddress('',cid,"#citys");
    }
}

//市区联动
function provices(obj){
    var pid= $("#provices").val();
    if(!pid){
        $("#provices").html("<option value=''>请选择市区</option>");
    }else{
        getAddress('',pid,"#citys");
    }
}

//当前地区选择的下拉渲染
function getAddress(ids = '',texts,types){
    $.ajax({
        url : '/pub/Address/getAll',
        data : {ids:ids,parentId:texts},
        type : "POST",
        async:false,
        success:function(data){
            $(types).html(data);
        }
    })
}


/**
 * 供应/分销商地区mapping条件搜索
 * @author jing 2019-07-08
 */

//供应/分销商地区
$("#supplier_add").on('click',function () {
    adressFun("#supplier_add",".supplier-add-box");
})

//profiy地区
$("#profit_address").on('click',function () {
    adressFun("#profit_address",".profit-box");
})

//供应商/profit地区显示
function adressFun(buttons,selectBoxs){
    $(buttons).on("input", (function () { //获取输入值
            if ($(this).val() != "") {
                getAjax($(this).val(),selectBoxs);
                $(selectBoxs).show();
            }else{
                $(selectBoxs).hide();
            }
        })
    ).focus(function () { //获取焦点
        if ($(this).val() != "") {
            getAjax($(this).val(),selectBoxs);
            $(selectBoxs).show();
        }else{
            $(selectBoxs).hide();
        }
    });
    //点击隐藏
    $(document).bind("click", function (e) {
        var target = $(e.target);
        if (target.closest(buttons).length == 0) {
            $(selectBoxs).hide();
        }
    })

}

//搜索框输入请求：
function getAjax(text,selectBoxs){
    var selectSons = selectBoxs.indexOf('supplier') != -1 ? 'supplier-add-son':'profit-ple';
    var types = selectBoxs.indexOf('supplier') != -1 ? 'mapping_add':'profit_add';
    $.ajax({
        url : '/admin/Mapping/fliggyInterAreaMappingList',
        data : {other:types,map_name:text},
        type : "POST",
        success:function(data){
            if(data.length){
                renderingSupplier(data,selectBoxs,selectSons);
            }else{
                $(selectBoxs).html('<li style="text-align:center;margin-top:3px;color:#adadad">暂无相关的记录</li>');
            }
        }
    })
}

